<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/21 10:25
 * Remark:
 */

namespace App\Models\Company;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Func\IpFunc;
use App\Func\TokenFuc;
use App\Models\BaseModel;
use App\Models\BaseModelV2;
use App\Scopes\DbHandleScope;
use Illuminate\Support\Facades\DB;

class CompanyModel extends BaseModelV2
{
    protected $table = TableConsts::CY_COMPANY_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_COMPANY_TABLE_PRIMARY_KEY;

    protected $attributes = [
        'status' => BaseConsts::STATUS_NORMAL,
    ];

    protected $fillable = [
        'company_id',
        'company_name',
        'company_parent_id',
        'status',
        'created_at',
        'updated_at',
    ];

    /**
     * 不需要走全局作用域
     */
    protected static function booted()
    {
       //  static::addGlobalScope(new DbHandleScope());
    }


    /**
     * 新增公司
     * @return bool
     */
    public function addCompanyModel($companyData): bool
    {
        return $this->save($companyData);
    }

    /**
     * 编辑公司
     * @param $companyData
     * @return bool
     */
    public function editCompanyEdit($companyData): bool
    {
        return $this->where('company_id',$companyData['company_id'])->update($companyData);
    }

    /**
     * 批量修改数据
     * @param $companyIds
     * @param $companyData
     * @return mixed
     */
    public function bulkDeleteCompany($companyIds,$companyData){
        return $this->whereIn('company_id',$companyIds)->update($companyData);
    }


    /**
     * 通过公司ID获取公司详情
     * @param int $companyId
     * @param string $select
     * @return mixed
     */
    public function getCompanyInfoModelByCompanyId(int $companyId,array $select = ["*"]): mixed
    {
        return $this->select($select)->find($companyId);
    }

    /**
     * 通过公司名称 获取公司详情
     * @param string $companyName
     * @param string $select
     * @return mixed
     */
    public function getCompanyInfoModelByCompanyName(string $companyName,array $select = ["*"]): mixed
    {
        return $this->select($select)->where('company_name',$companyName)->first();
    }


    /**
     * 获取公司下拉
     * @return mixed
     */
    public function getCompanySelect(): mixed
    {
        return $this->select(['company_id','company_id as key','company_name','company_parent_id'])
            ->where('company_id',TokenFuc::getCurrentStaffInfo()['company_id'])
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->get();
    }

    /**
     * 获取公司下拉
     * @return mixed
     */
    public function getCompanySelectByLogin(): mixed
    {
        return $this->select(['company_id','company_id as key','company_name','company_parent_id'])
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->get();
    }

    /**
     * 获取公司列表
     * @param $query
     * @param $pageSize
     * @return mixed
     */
    public function getCompanyList($query,$pageSize): mixed
    {
        return $query->where('status',BaseConsts::STATUS_NORMAL)
            ->latest()
            ->Paginate($pageSize);
    }

    public function getCompanyAllNextByCompanyId($companyId)
    {
        return $this
            ->select('company_id')
            ->where('company_level','LIKE',"%".$companyId."%")
            ->get();
    }


    public function getCompanyNextByCompanyId($companyId){
        return $this
            ->select('company_id')
            ->where('company_parent_id',$companyId)
            ->get();
    }


}